package prj.carmap.mobile.activity;

/*
 * Copyright 2013 Csaba Szugyiczki
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.List;

import com.actionbarsherlock.app.ActionBar.LayoutParams;
import com.actionbarsherlock.app.SherlockActivity;
import com.miaowang.salad.activity.MineInfoActivity;
import com.miaowang.salad.activity.SaladMainActivity;

import prj.carmap.mobile.CarmapApplication;
import prj.carmap.mobile.R;
import prj.carmap.mobile.model.SellerType;
import prj.carmap.mobile.model.UserInfo;
import prj.carmap.mobile.process.AutoServiceProcess;
import prj.carmap.mobile.process.LiveServiceProcess;
import prj.carmap.mobile.process.SellerByTypeProcess;
import prj.carmap.mobile.view.CircleLayout;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import prj.carmap.mobile.view.CircleLayout.OnItemClickListener;
import prj.carmap.mobile.view.CircleLayout.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class LiveActivity extends SherlockActivity implements
		OnItemSelectedListener, OnItemClickListener {
	TextView selectedTextView;
	final static String TAG = "LiveActivity";

	private List<SellerType> liveSellers;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_live);
		CarmapApplication.currentPage = "live";
		liveSellers = CarmapApplication.liveSellers;
		CarmapApplication.addActivity(this);

		CircleLayout circleMenu = (CircleLayout) findViewById(R.id.live_circle_layout);
		circleMenu.setOnItemSelectedListener(this);
		circleMenu.setOnItemClickListener(this);

		// Inflate the custom view
		View customNav = LayoutInflater.from(this).inflate(R.layout.nav_seller_title_view,
				null);
		// Attach to the action bar
		getSupportActionBar().setDisplayShowCustomEnabled(true);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT, Gravity.CENTER);
		getSupportActionBar().setCustomView(customNav);
		customNav.setLayoutParams(lp);
		Button returnBtn = (Button) customNav.findViewById(R.id.return_prev);
		returnBtn.setOnClickListener(new Button.OnClickListener() {// 创建监听
					public void onClick(View v) {
						// LiveActivity.this.startActivity(new Intent(
						// LiveActivity.this, MainActivity.class));
						LiveActivity.this.startActivity(new Intent(
								LiveActivity.this, MainActivity.class));
						((Activity) LiveActivity.this).finish();
					}
				});
		((TextView) customNav.findViewById(R.id.nav_title)).setText(this
				.getString(R.string.live_info));
		Button mapBtn = (Button) customNav.findViewById(R.id.return_map);
		mapBtn.setVisibility(View.GONE);
		initActionBar();
		initMenuItem();
		// selectedTextView =
		// (TextView)findViewById(R.id.main_selected_textView);
		// selectedTextView.setText(((CircleImageView)circleMenu.getSelectedItem()).getName());
	}

	@SuppressWarnings("deprecation")
	private void initMenuItem() {
		if ("live".equals(CarmapApplication.currentPage)) {
			// 底部菜单项
			View menuItem = this.findViewById(R.id.menu_item_type);
			menuItem.setBackgroundDrawable(getApplication().getResources()
					.getDrawable(R.drawable.bottom_background));
			// 底部菜单图标
			ImageView itemImage = (ImageView) this
					.findViewById(R.id.item_type_image);
			itemImage.setImageResource(R.drawable.type_select);
			// 底部菜单文字
			TextView itemText = (TextView) this
					.findViewById(R.id.item_type_text);
			itemText.setTextColor(getApplication().getResources().getColor(
					R.color.white));
		}
		if ("auto".equals(CarmapApplication.currentPage)) {
			// 底部菜单项
			View menuItem = this.findViewById(R.id.menu_item_home);
			menuItem.setBackgroundDrawable(getApplication().getResources()
					.getDrawable(R.drawable.bottom_background));
			// 底部菜单图标
			ImageView itemImage = (ImageView) this
					.findViewById(R.id.item_home_image);
			itemImage.setImageResource(R.drawable.front_page);
			// 底部菜单文字
			TextView itemText = (TextView) this
					.findViewById(R.id.item_home_text);
			itemText.setTextColor(getApplication().getResources().getColor(
					R.color.white));
		}
		// else if ("search".equals(CarmapApplication.currentPage)) {
		// // 底部菜单项
		// View menuItem = this.findViewById(R.id.menu_item_search);
		// menuItem.setBackgroundDrawable(getApplication().getResources()
		// .getDrawable(R.drawable.bottom_background));
		// // 底部菜单图标
		// ImageView itemImage = (ImageView) this
		// .findViewById(R.id.item_search_image);
		// itemImage.setImageResource(R.drawable.search_select);
		// // 底部菜单文字
		// TextView itemText = (TextView) this
		// .findViewById(R.id.item_search_text);
		// itemText.setTextColor(getApplication().getResources().getColor(
		// R.color.white));
		// }
		else if ("mine".equals(CarmapApplication.currentPage)) {
			// 底部菜单项
			View menuItem = this.findViewById(R.id.menu_item_mine);
			menuItem.setBackgroundDrawable(getApplication().getResources()
					.getDrawable(R.drawable.bottom_background));
			// 底部菜单图标
			ImageView itemImage = (ImageView) this
					.findViewById(R.id.item_mine_image);
			itemImage.setImageResource(R.drawable.mine_select);
			// 底部菜单文字
			TextView itemText = (TextView) this
					.findViewById(R.id.item_mine_text);
			itemText.setTextColor(getApplication().getResources().getColor(
					R.color.white));
		}
		else if ("salad".equals(CarmapApplication.currentPage)) {
			// 底部菜单项
			View menuItem = this.findViewById(R.id.menu_item_salad);
			menuItem.setBackgroundDrawable(getApplication().getResources()
					.getDrawable(R.drawable.bottom_background));
			// 底部菜单图标
			ImageView itemImage = (ImageView) this
					.findViewById(R.id.item_salad_image);
			itemImage.setImageResource(R.drawable.mine_select);
			// 底部菜单文字
			TextView itemText = (TextView) this
					.findViewById(R.id.item_salad_text);
			itemText.setTextColor(getApplication().getResources().getColor(
					R.color.white));
		}
		// else if ("more".equals(CarmapApplication.currentPage)) {
		// // 底部菜单项
		// View menuItem = this.findViewById(R.id.menu_item_more);
		// menuItem.setBackgroundDrawable(getApplication().getResources().getDrawable(R.drawable.bottom_background));
		// // 底部菜单图标
		// ImageView itemImage = (ImageView)
		// this.findViewById(R.id.item_more_image);
		// itemImage.setImageResource(R.drawable.more_select);
		// // 底部菜单文字
		// TextView itemText = (TextView)
		// this.findViewById(R.id.item_more_text);
		// itemText.setTextColor(getApplication().getResources().getColor(R.color.white));
		// }
	}

	private void initActionBar() {
		Log.d(TAG, "initActionBar");

		View view = (View) this.findViewById(R.id.menu_item_home);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AutoServiceProcess process = new AutoServiceProcess();
				process.setContext(LiveActivity.this);
				process.getSellerTypes();
			}

		});

		view = (View) this.findViewById(R.id.menu_item_mine);
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//				UserInfo userInfo = CarmapApplication.getInstance().userInfo;
//				if (userInfo == null) {
//					LiveActivity.this.startActivity(new Intent(
//							LiveActivity.this, LoginActivity.class));
//				} else {
//					LiveActivity.this.startActivity(new Intent(
//							LiveActivity.this, ProfileActivity.class));
//				}
				LiveActivity.this.startActivity(new Intent(
						LiveActivity.this, MineInfoActivity.class));
			}

		});
		
		View saladView = (View) this.findViewById(R.id.menu_item_salad);
		saladView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(LiveActivity.this, SaladMainActivity.class);
//				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				LiveActivity.this.startActivity(intent);
			}
			
		});


	}

	@Override
	public void onItemSelected(View view, int position, long id, String name) {
		// selectedTextView.setText(name);
	}

	@Override
	public void onItemClick(View view, int position, long id, String name) {
		// Toast.makeText(getApplicationContext(),
		// getResources().getString(R.string.start_app) + " " + name,
		// Toast.LENGTH_SHORT).show();
		Log.i("sss", "position：" + position);
		SellerType liveSeller = liveSellers.get(position);

		SellerByTypeProcess process = new SellerByTypeProcess();
		process.setContext(this);
		process.getSellerTypes(liveSeller.typeId + "", liveSeller.typeName);
		// process.getSellerTypes("22" + "", "汽车洗车");

		// switch (position) {
		// // 餐饮
		// case 0:
		// doFood();
		// break;
		// // 娱乐
		// case 1:
		// doAmusement();
		// break;
		// // 酒店
		// case 2:
		// doHotel();
		// break;
		// default:
		// break;
		// }

	}

	private void doHotel() {
		SellerByTypeProcess process = new SellerByTypeProcess();
		process.setContext(this);
		// process.getSellerTypes(autoSeller.typeId + "",
		// autoSeller.typeName);
		process.getSellerTypes("31" + "", "酒店");
	}

	private void doAmusement() {
		SellerByTypeProcess process = new SellerByTypeProcess();
		process.setContext(this);
		// process.getSellerTypes(autoSeller.typeId + "",
		// autoSeller.typeName);
		process.getSellerTypes("30" + "", "生活服务");
	}

	private void doFood() {
		SellerByTypeProcess process = new SellerByTypeProcess();
		process.setContext(this);
		// process.getSellerTypes(autoSeller.typeId + "",
		// autoSeller.typeName);
		process.getSellerTypes("28" + "", "餐饮");

	}
}
